package com.fzj.charging.order.rabbitmq;

import com.fzj.charging.order.config.RabbitMQConfiguration;
import com.fzj.charging.order.pojo.po.OrderMQPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMQProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(OrderMQPO orderMQPO){
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) ->{
            //correlationData:标识唯一的一条消息
            if(ack){
                log.debug("rabbitmq收到了生产者的消息，消息id：{}",correlationData);

            }else {
                log.debug("rabbitmq没有收到了生产者的消息，消息id：{},原因：{}",correlationData,cause);
                log.debug("重试，打日志；重试不成功，告警");
                log.debug("重试可能造成消息重复");
            }
        } );
        rabbitTemplate.convertAndSend(RabbitMQConfiguration.EXCHANGE_NAME,RabbitMQConfiguration.ROUTING_KEY,orderMQPO);
    }
}
